package com.gofunbuy.app.favourable;

import com.gofunbuy.R;
import com.gofunbuy.TabFavourableGroup;
import com.gofunbuy.TabStoreGroup;
import com.gofunbuy.util.GoFunBuyUtils;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SubTabFavourableDetailActivity extends Activity implements OnClickListener {
	//http://www.eye4gift.com/gofun/app_coupon_show.php?id=16

	TextView favourable_diet ;
	TextView favourable_private;
	TextView favourable_activity;
	TextView favourable_birth ;
	private WebView mWebView;
	
	private ProgressBar mProgressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favourable_detail_layout);
		init();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		webHtml();
		
		setSubTabColor(TabFavourableActivity.tabId);
	}
	private void webHtml() {  
		
		 new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {  	 
					if(!GoFunBuyUtils.isNetworkConnected(TabFavourableGroup.group)){
						Toast.makeText(TabFavourableGroup.group, "网络状况不好！", 2000).show();
						return;
					}
					String userId = GoFunBuyUtils.mUserID==null?"0":GoFunBuyUtils.mUserID;	
					mWebView.loadUrl("http://www.eye4gift.com/gofun/app_coupon_show.php?shopcode=goinside&id="+TabFavourableActivity.couponId+"&userid="+userId);
				 
				} catch (Exception ex) {  
				 
					ex.printStackTrace();  
				 
				}  
			}			 
		 }).start();
		
	}
	private void init(){
		
		TextView tvTitle = (TextView) findViewById(R.id.store_topbar_title);
		tvTitle.setText(R.string.title_favorable);
		
		TextView back = (TextView)findViewById(R.id.store_topbar_back);
		back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(TabFavourableActivity.class);
			}			
		});
		
		TextView buyList = (TextView) findViewById(R.id.store_topbar_buylist);
		buyList.setVisibility(View.GONE);

		mWebView = (WebView) findViewById(R.id.favourable_detail_web);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setWebViewClient(new WebViewClient(){       
            public boolean shouldOverrideUrlLoading(WebView view, String url) {       
                view.loadUrl(url);       
                return true;       
            }  
            public void onPageFinished(WebView view, String url){ 
            	mProgressBar.setVisibility(View.GONE);
            	mWebView.setVisibility(View.VISIBLE);
            } 
            public void onPageStarted(WebView view, String url, Bitmap favicon) { 
            	mProgressBar.setVisibility(View.VISIBLE);
            	mWebView.setVisibility(View.GONE);
            } 
            
		});
		
		favourable_diet  = (TextView) findViewById(R.id.favourable_diet);
		favourable_private = (TextView) findViewById(R.id.favourable_private);
		favourable_activity = (TextView) findViewById(R.id.favourable_activity);
		favourable_birth = (TextView) findViewById(R.id.favourable_birth);
		favourable_diet.setOnClickListener(this);
		favourable_private.setOnClickListener(this);
		favourable_activity.setOnClickListener(this);
		favourable_birth.setOnClickListener(this);
		
		mProgressBar = (ProgressBar)findViewById(R.id.favourable_detail_progress);
	}

	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(SubTabFavourableDetailActivity.this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabFavourableGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabFavourableGroup.group.setContentView(view);
	}
	public boolean onKeyDown(int keyCoder,KeyEvent event){
        if(mWebView.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK){
        	mWebView.goBack();   //goBack()表示返回webView的上一页面
            return true;
         }
         return false;
	}
	private void setSubTabColor(int tab){
		 TabFavourableActivity.tabId = tab;
		 favourable_diet.setTextColor(Color.WHITE) ;
		 favourable_private.setTextColor(Color.WHITE) ;
		 favourable_activity.setTextColor(Color.WHITE) ;
		 favourable_birth.setTextColor(Color.WHITE) ;
		switch (tab) {
		case 0:
			 favourable_diet.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;
		case 1:
			favourable_private.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;
		case 2:
			favourable_activity.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;
		case 3:
			favourable_birth.setTextColor(getResources().getColor(R.color.Orange)) ;
			break;		
		}
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub

		switch (view.getId()) {
		case R.id.favourable_diet:
			setSubTabColor(0);
			startActivity(TabFavourableActivity.class);
			
			break;
		case R.id.favourable_private:
			setSubTabColor(1);
			startActivity(TabFavourableActivity.class);
			
			break;
		case R.id.favourable_activity:
			setSubTabColor(2);
			startActivity(TabFavourableActivity.class);
			break;
		case R.id.favourable_birth:
			setSubTabColor(3);
			startActivity(TabFavourableActivity.class);
			break;		
		}
	}
	private void startActivity(Class<?> a,Bundle b) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(this, a);
		intent.putExtras(b);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabFavourableGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabFavourableGroup.group.setContentView(view);
		
	}
    private class LoadTask extends AsyncTask<Void, Void, Void> { 
    	
        @Override  
        protected void onPreExecute() {  
        	
        }            
        @Override  
        protected Void doInBackground(Void... params) {   
            
            return null;  
        }                   
          
        @Override  
        protected void onPostExecute(Void result) {  
        	
        }   
        @Override  
        protected void onCancelled() {  
        	
        }  
    }  
}